﻿using System;

namespace ThinNet.Common
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class ObjectExt
    {
        /// <summary>
        /// 转化为长整型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long ToLong(this object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            long.TryParse(obj.ToString(), out long l);
            return l;
        }

        /// <summary>
        /// 转化为整形
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt(this object obj)
        {
            if (obj == null)
            {
                return 0;
            }
            int.TryParse(obj.ToString(), out int i);
            return i;
        }

        /// <summary>
        /// 转化为 日期
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(this object obj)
        {
            if (obj == null)
            {
                return null;
            }
            if (DateTime.TryParse(obj.ToString(), out DateTime d))
            {
                return d;
            }
            return null;
        }
    }
}
